<template>
  <div class="login-page">
    <div class="login-container">
      <h1>自研光伏监控web端Demo用户登录</h1>
      <div id="login-form">
        <div class="form-group">
          <label for="username">用户名：</label>
          <input type="text" id="username" name="username" v-model="loginForm.username" required>
        </div>
        <div class="form-group">
          <label for="password">密码：</label>
          <input type="password" id="password" name="password" required v-model="loginForm.password">
        </div>
        <button @click="submit">登录</button>
      </div>
    </div>
  </div>
</template>
<script setup>
import { ElMessage } from 'element-plus'
import { ref, reactive } from "vue"
import { useStore } from 'vuex'
import { useRouter } from "vue-router";
const loginForm = reactive({
  username: '',
  password: ''
})
const router = useRouter()
const store = useStore()  // 该方法用于返回store 实例
const submit = () => {
  store.dispatch('user/Login', loginForm).then(res => {
    console.log('Login',res);
    if (res.success === true) { 
      let str = `Bearer ${res.data.token}`
      let userInfo = { ...res.data }
      sessionStorage.setItem('token', str)
      sessionStorage.setItem('userInfo', JSON.stringify(userInfo))
      ElMessage({
        message: '登录成功',
        type: 'success',
      })
      setTimeout(() => {
        router.replace('/test')
      }, 500)
    } else {
      ElMessage.error(res.msg);
    }
  }).catch(err => {
    console.log(err);
  })
}
</script>
<style lang="scss" scoped>
.login-page {
  font-family: Arial, sans-serif;
  background-color: #f4f4f4;
  margin: 0;
  padding: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  width: 100vw;
}

.login-container {
  background-color: #fff;
  padding: companyH(20vh) companyW(20vw);
  border-radius: 5px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  width: 50%;
  height: 50%;
}

h1 {
  margin: companyH(25vh) 0 companyH(20vh) 0;
  text-align: center;
}

.form-group {
  margin: companyH(80vh) 0;
  line-height: companyH(32vh);
}

label {
  display: block;
  font-weight: bold;
}

input {
  width: 100%;
  padding: companyH(10vh) companyW(10vw);
  border: 1px solid #ccc;
  border-radius: 5px;
  outline: none;
  box-sizing: border-box;
}

button {
  background-color: #007bff;
  color: #fff;
  border: none;
  border-radius: 5px;
  padding: companyH(10vh) companyW(20vw);
  cursor: pointer;
  position: relative;
  left: 50%;
  transform: translate(-50%, 0);
}
</style>
